-- probability_damage
-- created by xinj
-- 攻击概率造成额外伤害

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 对敌伤害触发
    trigger = function(who, prop, arg)
        if DungeonM.getRandSeed("probability_damage") % 1000 >= prop[3] then
            return 0;
        end

        local extraDamage = 0;
        if tonumber(prop[2]) ~= nil and tonumber(prop[2]) > 0 then
            extraDamage = math.modf(prop[2] * arg / 1000);
        else
            extraDamage = who:queryAttrib(prop[2]);
        end

        -- 触发吸血的标记
        PropM.trigger(who, "crit_absorb", 1);

        return math.max(0, extraDamage);
    end,

    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;

        local rate = tonumber(prop[3]) / 10;
        local add = 100 + tonumber(prop[2]) / 10;
        local multi, re = math.modf(add / 100);

        if re == 0 then
            -- 概率造成n倍伤害的
            -- 2显示为双倍，其他显示为数字
            if multi == 2 then
                desc = getLocStr("prop_addition_desc_412_1");
            else
                desc = getLocStr("prop_addition_desc_412_2");
                desc = string.gsub(desc, "{0}", multi);
            end
        elseif rate >= 100 then
            -- 必定造成额外伤害的
            add = add - 100;
            desc = row.append_desc;
            desc = string.gsub(desc, "{0}", add);
        else
            -- 默认显示
            desc = string.gsub(desc, "{0}", add);
        end

        desc = string.gsub(desc, "{1}", rate);
        return desc;
    end,

    record = function(target, prop, path)
        return false;
    end
};
